import { Controller, Post, Body, Param, Query, UseGuards, Get } from "@nestjs/common";
import { PubTaskService } from "./pub-task.service";
import { PubTemplateService } from "./pub-template.service";
import { PubTemplateEntity } from "@/db/entitys/pub-template.entity";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
import { IWonewSer } from "@/typing";

@Controller("pub")
@UseGuards(JwtAuthGuard)
export class PubController {
  constructor(
    private readonly pubTaskService: PubTaskService,
    private readonly pubTemplateService: PubTemplateService,
  ) {}

  @Get("/template/list")
  getTemplateList(@Query() opt?: IWonewSer.QueryFilterParams) {
    return this.pubTemplateService.getTemplateList(opt)
  }
  
  @Post("/template/create")
  createTemplate(@Body() body: Partial<PubTemplateEntity>) {
    return this.pubTemplateService.createOrUpdateTemplate(0, body)
  }

  @Post("/template/:id/update")
  updateTemplate(@Param("id") id: number, @Body() body: Partial<PubTemplateEntity>) {
    return this.pubTemplateService.createOrUpdateTemplate(id, body)
  }

  @Post("/template/:id/delete")
  deleteTemplate(@Param("id") id: number) {
    return this.pubTemplateService.deleteTemplate(id)
  }

  @Get("/task/list")
  getTaskList(@Query() opt?: IWonewSer.QueryFilterParams) {
    return this.pubTaskService.getTaskList(opt)
  }
  
  @Post("/task/create")
  createTask(@Body() body: Partial<PubTemplateEntity>) {
    return this.pubTaskService.createOrUpdateTask(0, body)
  }

  @Post("/task/:id/create_multiple")
  createTaskMultiple(@Param("id") id: number, @Body() body: {items: {title: string, video_bili_bvid: string}[]}) {
    return this.pubTaskService.createTaskMultiple(id, body.items)
  }

  @Post("/task/:id/update")
  updateTask(@Param("id") id: number, @Body() body: Partial<PubTemplateEntity>) {
    return this.pubTaskService.createOrUpdateTask(id, body)
  }

  @Post("/task/:id/delete")
  deleteTask(@Param("id") id: number) {
    return this.pubTaskService.deleteTask(id)
  }
  @Post("/task/delete_many")
  deleteManyTask(@Body() ids: number[]) {
    return this.pubTaskService.deleteManyTask(ids)
  }

  @Post("/task/:id/start")
  startTask(@Param("id") id: number, @Body() body: {selected_accounts?: string}) {
    return this.pubTaskService.startTask(id, body.selected_accounts)
  }

  @Post("/task/:id/get_video")
  getTaskVideo(@Param("id") id: number, @Body() config: any) {
    return this.pubTaskService.getTaskVideo(+id, config)
  }

  @Post("/task/get_video/async")
  getTaskVideoAsync(@Body() body: {config: any, ids: number[]}) {
    return this.pubTaskService.getTaskVideoAsync(body)
  }

  @Get("/task/get_video/queue")
  getTaskVideoQueue() {
    return this.pubTaskService.getTaskVideoQueue()
  }

  @Post("/task/:id/copy")
  copyTask(@Param("id") id: number) {
    return this.pubTaskService.copyTask(id)
  }

  @Get("/task/settings")
  getTaskSettings() {
    return this.pubTaskService.getTaskSettings()
  }

  @Post("/task/settings")
  setTaskSettings(@Body() body: any) {
    return this.pubTaskService.setTaskSettings(body)
  }
}